#include "windowgame.h"

WindowGame::WindowGame(QWidget *parent) :
	QWidget(parent) {
	ui.setupUi(this);

	model = new GameBoardModel(this);
	ui.tableView->setModel(model);
	ui.tableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
	ui.tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
	ui.tableView->setFixedHeight(ui.tableView->horizontalHeader()->height() + ui.tableView->verticalHeader()->length() + 2);
	ui.tableView->setFixedWidth(ui.tableView->verticalHeader()->width() + ui.tableView->horizontalHeader()->length() + 2);

	TaskStatusDelegate *delegate = new TaskStatusDelegate(ui.tableView);
	ui.tableView->setItemDelegate(delegate);

	connect(ui.tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(cellClicked(QModelIndex)));
	connect(ui.pushButton, SIGNAL(clicked()), ui.tableView, SLOT(reset()));
}

WindowGame::~WindowGame() {
}

void WindowGame::cellClicked(QModelIndex indexClicked) {
	if (waitingForHumanMove) {
		model->makeMove(indexClicked, property("currentPlayerProperty").toUInt());
		moveMade();
	}
}

void WindowGame::setStateMachine(QState* parentState) {
	statePlayer1 = new QState(parentState);
	statePlayer2 = new QState(parentState);
	statePlayer1->assignProperty(this, "isWaitingForHumanMove", true/*player1->isHuman()*/);
	statePlayer1->assignProperty(this, "currentPlayerProperty", 0/*player1->id()*/);
	statePlayer1->addTransition(this, SIGNAL(moveMade()), statePlayer2);
	stateCheck = new QState(parentState);
	statePlayer2->assignProperty(this, "currentPlayerProperty", 1);
	statePlayer2->addTransition(this, SIGNAL(moveMade()), statePlayer1);

	stateStart = new QState(parentState);
	stateStart->addTransition(statePlayer1);
	parentState->setInitialState(stateStart);
}

void WindowGame::playerMove() {
	if (!waitingForHumanMove) {

	}
}
